//
//  YDBaseTableViewController.m
//  IngCard
//
//  Created by kf on 2017/5/23.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDBaseTableViewController.h"
#import "YDTableViewDataSource.h"
#import "YDBaseView.h"
#import "YDBaseTableViewCell.h"

@interface YDBaseTableViewController ()<YDViewDelegate>
@property(nonatomic, assign) BOOL dataSourceInited;
@property(nonatomic,strong,readwrite) YDUITableView *tableView;
@end

@implementation YDBaseTableViewController


#pragma mark - for SubClass
//在init过程中调用的函数
- (void)didInitialized {
    [super didInitialized];
    
    self.dataSourceInited = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

-(void) initNBandSB
{
    [super initNBandSB];
}

-(void) initSubviews
{
    [super initSubviews];
    [self.view addSubview:self.tableView];
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.equalTo(self.view);
    }];
}

-(void) initDataSource
{
    [super initDataSource];
}
#pragma mark - Private

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (!self.dataSourceInited)
    {
        [self initDataSource];
        self.dataSourceInited = YES;
    }
}

- (void)dealloc {
    // 用下划线而不是self.xxx来访问tableView，避免dealloc时self.view尚未被加载，此时调用self.tableView反而会触发loadView
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}

#pragma mark - <QMUITableViewDataSource, QMUITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.dataSource numberOfSections];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.dataSource numberOfRowsWithSectionIndex:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id <YDViewDataSource> viewModel = [self.dataSource dataSourceAtIndexPath:indexPath];
    
    CGFloat height = .0f;
    if (viewModel)
    {
        height = [viewModel viewHeight];
    }
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *tableCell = nil;
    
    id <YDViewDataSource> viewModel = [self.dataSource dataSourceAtIndexPath:indexPath];
    if (viewModel)
    {
        YDBaseTableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([viewModel viewClass])];
        
        if (!cell)
        {
            cell = [[[viewModel viewClass] alloc] initForTableView:self.tableView
                                                     withStyle:UITableViewCellStyleSubtitle];
        }
        
        [cell setDataSource:viewModel];
        [cell setDelegate:self];
        tableCell = (UITableViewCell *)cell;
        tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return tableCell;
}

#pragma mark -  <YDViewDelegate>*/
-(void) viewDidSelected:(UIView *) view
                eventId:(NSInteger) eventId
               userInfo:(NSDictionary *) userInfo
{
    
}

#pragma mark - lazy Loading
- (YDTableViewDataSource *)dataSource
{
    if (!_dataSource)
    {
        _dataSource = [[YDTableViewDataSource alloc] initWithTableView:self.tableView];
    }
    return _dataSource;
}

-(YDUITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[YDUITableView alloc] initWithFrame:self.view.bounds style:self.style];
//        _tableView = [YDUITableView new];
//        _tableView.style = _style;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
@end
